package cn.tedu.serviceEdu.service.impl;

import cn.tedu.serviceEdu.entity.EduTeacher;
import cn.tedu.serviceEdu.mapper.EduTeacherMapper;
import cn.tedu.serviceEdu.query.TeacherQuery;
import cn.tedu.serviceEdu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author tedu.张信兰
 * @since 2023-05-08
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired(required = false)
    private EduTeacherMapper eduTeacherMapper;

    @Override
    public void pageQuery(Page<EduTeacher> page, TeacherQuery teacherQuery) {
        LambdaQueryWrapper<EduTeacher> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EduTeacher::getIsDeleted,0);
        lqw.orderByDesc(EduTeacher::getModifiedTime);
        lqw.like(null != teacherQuery.getName(),EduTeacher::getName,teacherQuery.getName());
        lqw.eq(null != teacherQuery.getLevel(),EduTeacher::getLevel,teacherQuery.getLevel());
        lqw.ge(null != teacherQuery.getBegin(),EduTeacher::getCreateTime,teacherQuery.getBegin());
        lqw.le(null != teacherQuery.getEnd(),EduTeacher::getCreateTime,teacherQuery.getEnd());

        eduTeacherMapper.selectPage(page,lqw);
    }

    @Override
    public List<EduTeacher> getIndexTeacherList() {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduTeacher::getCreateTime).last("limit 4");
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;

    }
}
